/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.admin.business;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import reportcenter.support.dbutil.JPAFactory;
import reportcenter.report.dominio.datasource.DataSource;
import reportcenter.report.dominio.report.Report;
import reportcenter.report.dominio.column.ReportColum;

/**
 *
 * @author renan_luz
 */
public class AdminReportDao {

    public List<Report> getAll() {
        EntityTransaction tx = JPAFactory.getInstance().getManager().getTransaction();
        tx.begin();

        List<Report> reports = null;
        try {

            System.out.println("pesquisando relatorios");
            Query query = JPAFactory.getInstance().getManager().createNamedQuery("Report.findAll");
            reports = query.getResultList();

            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }
        return reports;
    }

    public void add(Report rp) {
        EntityTransaction tx = JPAFactory.getInstance().getManager().getTransaction();
        tx.begin();
        try {

//            Query query = JPAFactory.getInstance().getManager().createNamedQuery("DataSource.findByIdD");
//            query.setParameter("idD", 1);
//            DataSource ds = (DataSource) query.getSingleResult();
//
//            Report rp = new Report();
            rp.setHoraFim(new Date());
            rp.setHoraIni(new Date());
//            rp.setIdD(ds);
//            rp.setIdR(30);
//            rp.setQuery("select nome from Renan");
//            rp.setSubtitle("Relatórios da aplicação Report Center");
//            rp.setTitle("Relação dos Relatórios da Aplicação");

            //Add column
            ReportColum rp_c1 = new ReportColum();
            rp_c1.setColumnDescr("Nr. Contrato");
            rp_c1.setColumnName("CONTRATO");
//            rp_c1.setIdRc(20);
            rp_c1.setIdR(rp);
            rp.addReportColumn(rp_c1);

            //Add column
            ReportColum rp_c2 = new ReportColum();
            rp_c2.setColumnDescr("Nr. Contrato");
            rp_c2.setColumnName("CONTRATO");
//            rp_c2.setIdRc(30);
            rp_c2.setIdR(rp);
            rp.addReportColumn(rp_c2);

            //Add column
            ReportColum rp_c3 = new ReportColum();
            rp_c3.setColumnDescr("Nr. Contrato");
            rp_c3.setColumnName("CONTRATO");
//            rp_c3.setIdRc(40);
            rp_c3.setIdR(rp);
            rp.addReportColumn(rp_c3);

            JPAFactory.getInstance().getManager().persist(rp);

            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }
    }

    public void delete(Report report) {
        EntityTransaction tx = JPAFactory.getInstance().getManager().getTransaction();
        tx.begin();
        try {
            
            JPAFactory.getInstance().getManager().remove(report);

            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }
    }
}
